<template>
  <div class="student-main">
    <el-card class="search-bar card" shadow="hover">
      <div slot="header">
        <span>搜索条件</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="search.name" class="input" size="small" placeholder="根据名字查询" />
        </el-col>
        <el-col :span="6">
          <el-date-picker
            v-model="search.startTime"
            class="input"
            size="small"
            type="date"
            placeholder="选择开始日期"
            value-format="yyyy-MM-dd"
          />
        </el-col>
        <el-col :span="6">
          <el-date-picker
            v-model="search.endTime"
            class="input"
            size="small"
            type="date"
            placeholder="选择结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-col>
        <el-col :span="6">
          <!-- <el-input v-model="search.name" size="small" class="input" placeholder /> -->
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 20px">
        <el-col :span="6">
          <el-select v-model="search.classId" placeholder="选择班级">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.className"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="6">
          <!-- <el-input v-model="search.name" size="small" class="input" placeholder /> -->
        </el-col>

        <el-col :span="6" :offset="6" style="text-align: right;">
          <el-button size="small" @click="doReset">重置</el-button>
          <el-button type="primary" size="small" @click="doSearch">查询</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 搜索部分结束, 数据表格开始 -->
    <el-card class="container card" shadow="never">
      <div slot="header">
        <span>表格数据</span>
      </div>
      <div slot="header">
        <el-button type="primary" @click="goAdd">新增</el-button>
      </div>
      <el-table v-loading="loading" :data="studentList" border style="width: 100%">
        <el-table-column label="#" type="index" width="50" fixed />
        <el-table-column prop="name" label="姓名" width="150" />
        <el-table-column prop="sexv" label="姓别" width="120" />
        <el-table-column prop="classname" label="班级" width="120" />
        <el-table-column prop="dormname" label="宿舍" width="120" />
        <el-table-column prop="cityText" label="城市" width="300" />
        <el-table-column prop="startTime" label="开始时间" width="120" />
        <el-table-column fixed="right" label="操作" width="100">
          <!-- 自定义列的实现  作用于插槽-->
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="goEdit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="goDele(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 -->
      <el-pagination
        style="margin-top: 30px;text-align:right;"
        :current-page="page.start"
        :page-sizes="page.sizes"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 这里是弹窗表单 -->
    <Detail ref="detail" @refresh-list="getStudentList"/>
  </div>
  
</template>

<script>
import { classList } from "@/api/xy/clazz";
import Detail from './Detail'
import {
  studentList,
  studentDetail,
  studentSave,
  studentUpdate,
  dormList,
  dele,
} from "@/api/xy/student";
import { pickBy } from "@/utils/index";

export default {
  components:{
    Detail
  },
  name: "XyStudent",
  data() {
    return {
      loading: false,
      title: "学生管理",
      search: {},
      classList: [],
      studentList: [], // 学生列表    
      page: {
        start: 1,
        limit: 10,
        totalCount: 0,
        sizes: [10, 20, 30], // 注意: element 的sizes必须是limit的整倍数
      },
    };
  },
  created() {
    // 初始化班级列表
    this.getClassList();
    // 初始化班级列表
    this.getStudentList();
    this.getDormList();
  },
  methods: {
    
    goDele(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dele({ id: id });
          this.getClassList();
          this.getStudentList();
          this.getDormList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    doReset() {},
    doSearch() {
      this.page.start = 1;
      this.getStudentList();
    },
    goAdd() {
      this.$refs.detail.openDialog();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // 页码发生了变化 需要改变start重新发送请求
      console.log(`当前页: ${val}`);
      this.page.start = val;
      this.getStudentList();
    },

    goEdit(id) {
      this.$refs.detail.openDialogDetail(id);
      
      studentDetail({
        id,
      }).then((res) => {
        // res.data.id = id;
        this.student = res.data;
       
    
        
      });
    },
    getStudentList() {
      this.loading = true;
      var params = {
        start: this.page.start,
        limit: this.page.limit,
        ...this.search, //啥解构赋值
      };
      params = pickBy(params);
      studentList(params).then((res) => {
        this.studentList = res.data.list;
        this.page.totalCount = res.data.totalRow;
        this.loading = false;
      });
    },
    getDormList() {
      dormList({limit:1000}).then((res) => {
        this.dormList = res.data.list;
      });
    },
    getClassList() {
      classList({limit:10000}
        // start: this.page.start,
        // limit: this.page.limit,
      ).then((res) => {
        this.classList = res.data.list;
        console.log(res);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.student-main {
  .card {
    margin: 30px;
  }
  .search-bar {
    ::v-deep .el-input__inner {
      width: 220px;
    }
  }
}
</style>
